package com.bash.carrep.ui;

import com.bash.carrep.dao.enties.CreditLevel;
import com.bash.carrep.ui.datasource.UserContainer;
import com.vaadin.data.Property;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;

/**
 *
 * @author p.bashizi
 */
public class UserTable extends Table {

    public UserTable(CarrepApplication app) {

        setSizeFull();
        setContainerDataSource(UserContainer.getData());

        addGeneratedColumn("creditLevel", new CreditCulumnGenerator());

        setVisibleColumns(UserContainer.NATURAL_COL_ORDER);
        setColumnHeaders(UserContainer.COL_HEADERS_ENGLISH);

        setColumnCollapsingAllowed(true);
        setColumnReorderingAllowed(true);

        /*
         * Make table selectable, react immediatedly to user events, and pass
         * events to the controller (our main application)
         */
        setSelectable(true);
        setImmediate(true);
        addListener((ValueChangeListener) app);
        /* We don't want to allow users to de-select a row */
        setNullSelectionAllowed(false);
    }

    class CreditCulumnGenerator implements Table.ColumnGenerator {

        @Override
        public Component generateCell(Table source, Object itemId, Object columnId) {
            // Get the object stored in the cell as a property

            CreditLevel cl;
            Label l;
            Property prop =
                    source.getItem(itemId).getItemProperty(columnId);

            if (prop.getType() == CreditLevel.class) {
                cl = (CreditLevel) prop.getValue();

                l = new Label(cl.getIdcreditLevel() + "");

                return l;
            }
            return null;
        }
    }
}